import * as fs from 'fs';
import * as path from 'path';

interface FileNode {
    name?: string;
    children?: FileNode[];
    size?: number;
    type?: 'dir' | 'file';
    content?: string;
}

// 定义读取文件夹并保存文件结构的函数
export function readDirectory(dirPath: string, fileStructure: FileNode = {}): FileNode {
    const files = fs.readdirSync(dirPath);
    console.log(files,fileStructure);
    if (!fileStructure.name) {
        console.log(path.basename(dirPath));
        fileStructure = {
            name: path.basename(dirPath),
            type: 'dir',
            children: [],
        };
        console.log(fileStructure);
    };


    files.forEach((file, index) => {
        const fullPath = path.join(dirPath, file);
        const fileStat = fs.statSync(fullPath);

        if (fileStat.isDirectory()) {
            if (index > 0) {
                fileStructure.children.push(
                    {
                        name: file,
                        type: 'dir',
                        children: [],
                    }
                )
                const nextDir = fileStructure.children[fileStructure.children.length - 1];
                readDirectory(fullPath, nextDir as FileNode);
            } else {
                fileStructure.children.push(
                    {
                        name: file,
                        type: 'dir',
                        children: [],
                    })
                const nextDir = fileStructure.children[fileStructure.children.length - 1];
                readDirectory(fullPath, nextDir as FileNode);
            }
        } else {
            fileStructure.children.push(
                {
                    name: file,
                    type: 'file',
                    // size: fileStat.size,
                    content: fs.readFileSync(fullPath, 'utf-8'),
                }
            )
        }
    });
    return fileStructure;
}

export function createDirectoryFromStructure(structure: FileNode, basePath: string) {
    console.log(structure);
    if (structure.type === 'dir') {
        let folderPath = path.join(basePath, structure.name);
        if (fs.existsSync(folderPath)) {
            folderPath = folderPath + `_${new Date().getTime()}`;
        }
        // 创建目录
        fs.mkdirSync(folderPath, { recursive: true });
        // 递归创建子项
        structure.children.forEach(child => {
            createDirectoryFromStructure(child, folderPath);
        });
    } else if (structure.type === 'file') {
        // 创建文件
        fs.writeFileSync(path.join(basePath, structure.name), structure.content);
    }
}

export const _fileStructure: FileNode = {
    "name": "new-project-rust",
    "type": "dir",
    "children": [
      {
        "name": "Cargo.toml",
        "type": "file",
        "content": "[package]\nname = \"new_project\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\n"
      },
      {
        "name": "src",
        "type": "dir",
        "children": [
          {
            "name": "main.rs",
            "type": "file",
            "content": "fn main() {\n    println!(\"Hello, World!\");\n}"
          },
          {
            "name": "module1.rs",
            "type": "file",
            "content": "pub fn print_hello() {\n    println!(\"Hello from module1!\");\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n\n    #[test]\n    fn test_print_hello() {\n        print_hello();\n    }\n}\n"
          }
        ]
      }
    ]
  }
  
